// pages/mine/mine/mine.js
const app = getApp();
var url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    var that = this;
    console.log(app.globalData.userid)
    const userid = app.globalData.userid;
    if (app.globalData.userInfo != null) {
      const name = app.globalData.userInfo.nickName;
      wx.request({
        url: app.globalData.url + 'isExitMemberByName',
        data: {
          name: name
        },
        header: {
          "Content-Type": "application/json"
        },
        method: 'GET',
        success: res => {
          if (res.data.object != 0) {
            wx.request({
              url: url + 'getMemerUserById',
              data: {
                userId: res.data.object
              },
              header: {
                "Content-Type": "application/json"
              },
              success: function (res) {
                that.setData({
                  user: res.data.object
                })
              },
            })
          } else {
            wx.request({
              url: url + 'registerByWeChart',
              data: {
                userName: app.globalData.userInfo.nickName,
                memberImg: app.globalData.userInfo.avatarUrl
              },
              header: {
                "Content-Type": "application/json"
              },
              success: function (res) {
                app.globalData.userid = res.data.object
                that.setData({
                  userid: res.data.object,
                  members: res.data.object
                })
              }
            })
          }
        },
      })
    }
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          console.log(res.userInfo)
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  //我的订单
  orders: function () {
    wx.navigateTo({
      url: '../order/order',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //购物车
  AllShopping:function(){
    wx.navigateTo({
      url: '../allShoppingCar/allShoppingCar',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //我的会员
  members: function () {
    wx.navigateTo({
      url: '../members/members',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //我的卡包
  card: function () {
    wx.navigateTo({
      url: '../card/development/development',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //我的收藏
  collection: function () {
    wx.navigateTo({
      url: '../collection/collection',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //我的地址
  address: function () {
    wx.navigateTo({
      url: '../address/address'
    })
  },
  // 我的消息
  message: function () {
    wx.navigateTo({
      url: '../message/message'
    })
  },
  // 系统设置
  system: function () {
    wx.navigateTo({
      url: '../system/system',
    })
  },
  //个人信息
  personal: function () {
    wx.navigateTo({
      url: '../personal/personal',
    })
  },
  //客服中心
  problems: function () {
    wx.navigateTo({
      url: '../problems/problems'
    })
  },
})